/*
 * 线上环境代码编译
 * @author li.xiaomei
 * @date 2016-12-19
 */
require('shelljs/global');

var ora = require('ora')
var path = require('path')
var webpack = require('webpack')
var webpackConfig = require('../util/webpack/prod.js')
var config = global.think_config

module.exports = function(){
  var spinner = ora('开始编译打包~~~...');
  spinner.start();

  var assetsPath = path.join(config.webpack.build.assetsRoot, config.init.zip)
  rm('-rf', assetsPath)
  //mkdir('-p', assetsPath)
  //ls('*.js')
  //cp('-R', 'static/', assetsPath)

  webpack(webpackConfig(), function (err, stats) {
    spinner.stop()
    if (err) throw err
    process.stdout.write(stats.toString({
      colors: true,
      modules: false,
      children: false,
      chunks: false,
      chunkModules: false
    }) + '\n')

    if(config.init.cpPath) {
      //复制文件
      var fileArr = config.init.filePath;
      if(fileArr && fileArr.length) {
        for(var k = 0, len = fileArr.length; k < len; k++) {
          cp('-R', config.init.zip + '/' + fileArr[k], config.init.cpPath + '/' + fileArr[k]);
        }
      }else {
        //复制目录
        cp('-R', config.init.zip+'/*', config.init.cpPath);
      }

    }

  })

}
